package com.gotop.system.controller;

import com.gotop.common.annotation.ApiLog;
import com.gotop.common.base.BaseController;
import com.gotop.common.config.annotation.PreAuth;
import com.gotop.common.constant.RoleConstant;
import com.gotop.common.exception.BaseException;
import com.gotop.common.utils.R;
import com.gotop.common.utils.auth.CemUser;
import com.gotop.entity.system.Menu;
import com.gotop.system.service.system.IMenuService;
import com.gotop.system.wrapper.MenuWrapper;
import com.gotop.vo.system.MenuVO;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @program: cem
 * @description: 菜单
 * @author: superfish
 * @create: 2020-12-22 10:15
 **/
@RestController
@AllArgsConstructor
@RequestMapping("menu")
@Api(value = "菜单", tags = "菜单")
public class MenuController extends BaseController {

    @Autowired
    private IMenuService menuService;

    /**
     * 列表
     */
    @GetMapping("/list")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "菜单编号", paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "name", value = "菜单名称", paramType = "query", dataType = "string")
    })
    @ApiLog
    @PreAuth(RoleConstant.HAS_ROLE_ADMIN)
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "列表", notes = "传入menu")
    public R<List<MenuVO>> list(@ApiIgnore @RequestParam Map<String, Object> menu) {
        List<Menu> list = menuService.list(menu);
        return R.data(MenuWrapper.build().listNodeVO(list));
    }

    /**
     * 新增或修改
     */
    @ApiLog
    @PostMapping("/submit")
    @PreAuth(RoleConstant.HAS_ROLE_ADMIN)
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "新增或修改", notes = "传入menu")
    public R submit(@Valid @RequestBody Menu menu) {
        return R.status(menuService.saveOrUpdate(menu));
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    @PreAuth(RoleConstant.HAS_ROLE_ADMIN)
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "删除", notes = "传入ids")
    public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
        final String[] split = ids.split(",");
        return R.status(menuService.removeByIds(Arrays.asList(split)));
    }


    /**
     * 详情
     */
    @ApiLog
    @GetMapping("/detail")
    @PreAuth(RoleConstant.HAS_ROLE_ADMIN)
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "详情", notes = "传入menu")
    public R<MenuVO> detail(Menu menu) {
        Menu detail = menuService.getOne(menu);
        return R.data(MenuWrapper.build().entityVO(detail));
    }

    /**
     * 前端菜单数据
     */
    @GetMapping("/routes")
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "前端菜单数据", notes = "前端菜单数据")
    public R<List<MenuVO>> routes() {
        try {
            final CemUser user = user();
            List<MenuVO> list = menuService.routes((user == null || user.getUserId() == 0L) ? null : user.getRoleId());
            return R.data(list);
        } catch (BaseException e) {
            return R.data(e);
        }
    }

    /**
     * 前端按钮数据
     */
    @GetMapping("/buttons")
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "前端按钮数据", notes = "前端按钮数据")
    public R<List<MenuVO>> buttons() {
        final CemUser user = user();
        List<MenuVO> list = menuService.buttons(user.getRoleId());
        return R.data(list);
    }

    /**
     * 获取菜单树形结构
     */
    @GetMapping("/tree")
    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "树形结构", notes = "树形结构")
    public R<List<MenuVO>> tree() {
        List<MenuVO> tree = menuService.tree();
        return R.data(tree);
    }

    /**
     * 获取权限分配树形结构
     */
    @GetMapping("/grant-tree")
    @ApiOperationSupport(order = 8)
    @ApiOperation(value = "权限分配树形结构", notes = "权限分配树形结构")
    public R<List<MenuVO>> grantTree() {
        final CemUser user = user();
        return R.data(menuService.grantTree(user));
    }

    /**
     * 获取权限分配树形结构
     */
    @GetMapping("/role-tree-keys")
    @ApiOperationSupport(order = 9)
    @ApiOperation(value = "角色所分配的树", notes = "角色所分配的树")
    public R<List<String>> roleTreeKeys(String roleIds) {
        return R.data(menuService.roleTreeKeys(roleIds));
    }

}
